package com.test.dao;

import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.BorrowDetails;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface BookMapper {
    @Select("select * from book")
    List<Book> allBook();

    @Delete("delete from book where bid=#{bid}")
    int deleteBook(@Param("bid") int id);

    @Insert("insert into book(title,description,price) values(#{book.title},#{book.description},#{book.price})")
    void insertBook(@Param("book") Book book);

    @Insert("insert into borrow(bid,sid,time) values(#{bid},#{sid},NOW())")
    void insertBorrow(@Param("bid") int bid, @Param("sid") int sid);

    @Select("select * from borrow")
    List<Borrow> getAllBorrow();

    @Select("select * from borrow where sid=#{sid}")
    List<Borrow> getAllBorrowBySid(int sid);

    @Select("select * from book where bid=#{bid}")
    Book selectByBid(int bid);

    @Delete("delete from borrow where bid=#{bid} and sid=#{sid}")
    void deleteBorrowById(@Param("bid") int bid, @Param("sid") int sid);

    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column = "name",property ="user_name"),
            @Result(column="昵称",property="users_name"),
            @Result(column="title",property="book_title"),
            @Result(column="time",property="time")
    })
    @Select("select b.id,s.`name`,k.title,b.time,u.`name`\"昵称\" from borrow b left join student s on b.sid=s.sid left join book k on b.bid=k.bid left join users u on s.uid=u.id")
    List<BorrowDetails> borrowDetailsList();
}
